#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int minPathSum(vector<vector<int>>& grid)
    {
        const int INF = 0x3f3f3f3f;
        int m = grid.size(), n = grid[0].size();
        vector<vector<int>> dp(m + 1, vector<int>(n + 1, INF));
        dp[0][1] = 0;

        for (int i = 1; i <= m; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + grid[i - 1][j - 1];
            }
        }
        return dp[m][n];
    }
};
class Solution {
public:
    int uniquePaths(int m, int n)
    {
        vector<vector<int>> dp(m, vector<int>(n, 1));
        for (int i = 1; i < m; i++)
        {
            for (int j = 1; j < n; j++)
            {
                dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
            }
        }
        return dp[m - 1][n - 1];
    }
};
class Solution {
public:
    int longestValidParentheses(string s)
    {
        int n = s.size();
        vector<int> dp(n, 0);
        int ret = 0;

        for (int i = 1; i < n; i++)
        {
            if (s[i] == ')')
            {
                if (i - dp[i - 1] - 1 >= 0 && s[i - dp[i - 1] - 1] == '(')
                {
                    dp[i] = 2 + dp[i - 1] + (i - dp[i - 1] - 2 >= 0 ? dp[i - dp[i - 1] - 2] : 0);
                }
            }
            ret = max(ret, dp[i]);

        }

        return ret;
    }
};
class Solution {
public:
    bool canPartition(vector<int>& nums)
    {
        int m = nums.size();
        int sum = 0;
        for (auto num : nums) sum += num;

        if (sum % 2 == 1) return false;

        vector<bool> dp(sum / 2 + 1);
        dp[0] = true;

        for (int i = 1; i <= m; i++)
        {
            for (int j = sum / 2; j >= nums[i - 1]; j--)
            {
                dp[j] = dp[j] || dp[j - nums[i - 1]];
            }
        }

        return dp[sum / 2];
    }
};